﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodunitServices : Repository<corecmsfoodunit>, IcorecmsfoodunitServices
    {
        public bool InsertAsync(corecmsfoodunit entity)
        {
            return base.Change<corecmsfoodunit>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodunit> entity)
        {
            return base.InsertOrUpdate(entity);
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodunit>().Update(a => new corecmsfoodunit()
            {
                delFlag = 0
            }, a => a.uomId == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodunit>().Update(a => new corecmsfoodunit()
                {
                    delFlag = 0
                }, a => a.uomId == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodunit> GetCaChe()
        {
            return base.GetList();
        }

        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodunit> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodunit> custos = new List<corecmsfoodunit>();
            var where = Expressionable.Create<corecmsfoodunit>();
            where = where.And(a => a.delFlag != 0);
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.uomId).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.uomId).ToList();
            }
            return custos;
        }
        #endregion

        #region 根据类型查询数据
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodunit> QueryAsync(long typeId)
        {
            List<corecmsfoodunit> list = new List<corecmsfoodunit>();
            var where = Expressionable.Create<corecmsfoodunit>();
            where = where.And(a => a.delFlag == 1 && a.uomId > 0);
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.uomId).ToList();
            return list;
        }
        #endregion
    }
}